//弹出层函数
var fw_dialog = Fw.cc({
	init : function(box, isstep){
		this.isstep = isstep;
		this.x = "center";
		this.y = "middle";
		this.pos = 0;
		this.timer = null;
		this.time = 0;
		this.box = $(box);
		if(!this.box){
			this.box = $(document.body).append("div");
		}
		return this;
	},
	//增加遮照功能
	setmask : function(mk){
		if(!mk){
			mk = document.createElement("div");
			var css = "position:absolute; left:0px; top:0px; background-color:#cccccc; display:none;";
			css += "opacity:0.5; filter:alpha(opacity=50); width:100%; height:100%;";
			mk.style.cssText = css;
			//document.body.appendChild(mk);
			document.body.insertBefore(mk, this.box.obj);
		}
		this.mk = Fw.obj(mk);
		return this;
	},	
	//设置拖动功能
	setdrag : function(hander){
		Fw.drag(this.box.obj, hander);
		return this;
	},
	//设置延时关闭的时间
	settime : function(t){
		this.time = t;
		return this;
	},
	//设置延时关闭的时间
	setpos : function(y,x,pos){
		this.y = y;		
		this.x = x;
		if(pos) this.pos=pos;
		return this;
	},	
	//设置打开按钮
	setopen : function(trigger){
		Fw.obj(trigger).onclick = this.open.bind(this);
		return this;
	},
	//设置关闭按钮
	setclose : function(trigger){
		Fw.obj(trigger).onclick = this.close.bind(this);
		return this;
	},
	//打开文件
	openFromFile : function(url){
		this.box.html('<iframe src="'+url+'" />');
		this.open();
	},
	//打开弹出框
	open : function(obj,e){
		var This = this;
		var d = document.documentElement;
		if(this.mk){
			var h = Math.max(document.body.offsetHeight,d.offsetHeight,d.clientHeight);
			this.mk.style.height = h + "px";
			this.mk.style.display = "block";
		}
		this.box.show();
		var w = this.box.obj.offsetWidth;
		var h = this.box.obj.offsetHeight;
		if(this.pos==0){
			var bw = Math.max(d.clientWidth, document.body.clientWidth);
			var bh = Math.max(d.clientHeight, document.body.clientHeight);
			var y = this.y=="top" ? 0 : this.y=="bottom" ? bh-h : this.y=="middle" ? Math.floor(bh/2-h/2) : this.y;
			var x = this.x=="left" ? 0 : this.x=="right" ? bw-w : this.x=="center" ? Math.floor(bw/2-w/2) : this.x;
		}else if(this.pos==1){
			e = window.event || e;
			var my = e.clientY;			
			var mx = e.clientX;
			var y = this.y=="top" ? my-h : this.y=="bottom" ? my : this.y=="middle" ? Math.floor(my-h/2) : my+this.y;
			var x = this.x=="left" ? mx-w : this.x=="right" ? mx : this.x=="center" ? Math.floor(mx-w/2) : mx+this.x;
		}		
		this.box.css({x:x,y:y+d.scrollTop});
		if(this.time>0){
			this.timer = window.setTimeout(function(){This.close()},this.time);
		}
		this.onopen && this.onopen(obj);
		return this;
	},
	//关闭弹出框
	close : function(obj){
		var This = this;		
		this.time>0 && window.clearTimeout(this.timer);
		if(this.isstep){
			var layer = this.box;
			var mk = this.mk;
			var t;
			var n = 1;
			var f = function(){
				n -= 0.1;
				if(n<0){
					clearTimeout(t);
					Fw.setOpacity(layer, 1);
					layer.hide();					
					if(mk){
						Fw.setOpacity(mk, 0.5);
						mk.style.display = "none";
					}
					This.onclose && This.onclose(obj);
					return;
				}
				Fw.setOpacity(layer, n);
				mk && Fw.setOpacity(mk, n*0.5);
				t = setTimeout(f, 24);
			};
			f();
		}else{
			if(this.mk) this.mk.style.display = "none";
			this.box.hide();
			this.onclose && this.onclose(obj);
		}
		return this;
	}
});